package com.base.andlanggl.programs

import android.opengl.GLES20.*
import com.base.andlanggl.util.LangGLTools

/**
 * 颜色着色器
 */
class ColorShaderProgram{
    /**
     * attribute 把属性放进着色器的手段
     * uniform 把属性放进着色器的手段 mat4 4*4矩阵
     * vec4 4个分量坐标 xyzw 默认值分别0001
     * varying 混合颜色 将顶点颜色进行混合
     * gl_PointSize 点的大小
     */
    private val vertexShader:String ="""
        uniform mat4 u_Matrix;
        attribute vec4 a_Position;
        attribute vec4 a_Color;
        varying vec4 v_Color;
        void main(){
            v_Color=a_Color;
            gl_Position= u_Matrix*a_Position;
            gl_PointSize= 10.0;
        }"""

    /**
     *  浮点型精度 mediump ，默认高精度
     *  varying 4个分量rgba 接收顶点着色器传过来的数据
     */
    private val fragmentShader:String ="""
        precision mediump float;
        varying vec4 v_Color;
        void main(){
            gl_FragColor= v_Color;
        }"""

    private val glProgram:Int= LangGLTools.createProgram(vertexShader,fragmentShader)
    val uMatrixLocation:Int= glGetUniformLocation(glProgram, "u_Matrix")
    val aPositionLocation:Int= glGetAttribLocation(glProgram, "a_Position")
    val aColorLocation:Int=
        glGetAttribLocation(glProgram, "a_Color")
    //设置Uniform并返回属性位置
    fun setUniforms(matrix:FloatArray){
        //传入矩阵数据
        glUniformMatrix4fv(uMatrixLocation, 1, false, matrix, 0)
    }
    fun useProgram(){
        glUseProgram(glProgram)//使用程序
    }
}